package com.example.conusmer.controller

import com.example.conusmer.entity.User
import com.example.conusmer.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/user")
class UserController() {
    @Autowired
    lateinit var userService: UserService

    @GetMapping
    fun getAllItems(): List<User> = userService.list()

    @GetMapping("/{id}")
    fun getItemById(@PathVariable id: String): ResponseEntity<User?> = ResponseEntity.ok(userService.getById(id))

    @PostMapping
    fun createItem(@RequestBody item: User): ResponseEntity<User> {
        userService.create(item)
        return ResponseEntity.noContent().build()
    }

    @PutMapping()
    fun updateItem(@RequestBody item: User): ResponseEntity<User> {
        userService.update(item)
        return ResponseEntity.noContent().build()
    }

    @DeleteMapping("/{id}")
    fun deleteItem(@PathVariable id: String): ResponseEntity<Void> {
        userService.delete(id)
        return ResponseEntity.noContent().build()
    }

}